﻿using HCQ2_Common;
using HCQ2_Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using HCQ2_Model.ViewModel;
using HCQ2_Model.VRDevice.ParamModel;
using HCQ2_Model.VRDevice.ResultModel;
using System.Web;
using System.IO;
using System.Diagnostics;
using HCQ2_Model.VRDD.OutputDto._211217;

namespace HCQ2UI_Logic.DocumentController
{
    /// <summary>
    ///  VR楼宇管理
    /// </summary>
    public class VRBuildingController : BaseLogic
    {
        //**************************1.0 VR楼宇*****************************
        #region 1.0 初始化Table + ActionResult InitTable()
        /// <summary>
        ///  初始化Table
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult InitTable(VRdeviceParam model)
        {
            if (model.folder_id == 0)
                return null;
            int total = 0;
            model.user_id = operateContext.Usr.user_id;
            List<VRBuildResult> list = operateContext.bllSession.T_DocumentInfo.GetVRBuildList(model,out total);
            TableModel tModel = new TableModel()
            {
                total = total,
                rows = list
            };
            return Json(tModel, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 1.1 添加楼宇 + ActionResult AddBuild(BuildParam param)
        /// <summary>
        ///  添加楼宇
        /// </summary>
        /// <param name="user">用户对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddBuild(BuildParam param)
        {
            ModelState.Remove("file_id");
            if (!ModelState.IsValid)
                return operateContext.RedirectAjax(1, getErrorMessage(ModelState), "", "");
            //添加设备
            T_DocumentInfo file = new T_DocumentInfo
            {
                file_name = param.file_name,
                alias_name = param.alias_name,
                programId = param.programId,
                file_code = param.file_code,
                file_note = param.file_note,
                file_order = param.file_order,
                file_status = 5,
                file_classify = "",
                doc_type = "",
                folder_num = "",
                page_type = "VRBuilding",
                doc_number = param.doc_number,
                create_time = DateTime.Now,
                create_id = operateContext.Usr.user_id,
                create_name = operateContext.Usr.user_name,
                update_name = operateContext.Usr.user_name,
                update_time = DateTime.Now
            };
            int mark = operateContext.bllSession.T_DocumentInfo.Add(file);
            if(mark<=0)
                return operateContext.RedirectAjax(1, "添加失败！", "", "");
            //添加：楼宇导航树关系
            T_DocumentFolderRelation relation = new T_DocumentFolderRelation
            {
                file_id = file.file_id,
                folder_id = param._folder_id,
                folder_path = param._folder_path,
                create_id = operateContext.Usr.user_id
            };
            operateContext.bllSession.T_DocumentFolderRelation.Add(relation);
            //添加：价格
            if (param.price > 0)
                operateContext.bllSession.T_VRGoodDisiPrice.Add(new T_VRGoodDisiPrice {
                    op_code = Helper.GetGuid,
                    file_code = param.file_code,
                    file_id = file.file_id,
                    op_month = 1,
                    ori_price = param.price,
                    price_name = "",
                    price_type = 1
                });        
            return operateContext.RedirectAjax(0, "添加楼宇成功~", "", "");
        }
        #endregion

        #region 1.2 编辑楼宇 + ActionResult EditBuild(BuildParam param)
        /// <summary>
        ///  编辑楼宇
        /// </summary>
        /// <param name="user">用户对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditBuild(BuildParam param)
        {
            if(param.file_id<=0)
                return operateContext.RedirectAjax(1,"楼宇id异常！", "", "");
            if (!ModelState.IsValid)
                return operateContext.RedirectAjax(1, getErrorMessage(ModelState), "", "");
            //添加设备
            T_DocumentInfo file = new T_DocumentInfo
            {
                file_name = param.file_name,
                alias_name = param.alias_name,
                programId = param.programId,
                file_code = param.file_code,
                file_note = param.file_note,
                file_order = param.file_order,
                doc_number = param.doc_number,
                update_name=operateContext.Usr.user_name,
                update_time=DateTime.Now,
            };
            operateContext.bllSession.T_DocumentInfo.Modify(file,s=>s.file_id==param.file_id, "file_name", "file_code", "file_note", "file_order", "update_name", "update_time", "doc_number", "alias_name", "programId");
            //修改：设备导航树关系
            T_DocumentFolderRelation relation = new T_DocumentFolderRelation
            {
                folder_id = param._folder_id,
                folder_path = param._folder_path
            };
            operateContext.bllSession.T_DocumentFolderRelation.Modify(relation, s => s.file_id == param.file_id, "folder_id", "folder_path");
            //修改：价格明细
            operateContext.bllSession.T_VRGoodDisiPrice.Delete(s => s.file_id == param.file_id);
            if (param.price > 0)
                operateContext.bllSession.T_VRGoodDisiPrice.Add(new T_VRGoodDisiPrice
                {
                    op_code = Helper.GetGuid,
                    file_code = param.file_code,
                    file_id = file.file_id,
                    op_month = 1,
                    ori_price = param.price,
                    price_name = "",
                    price_type = 1
                });
            return operateContext.RedirectAjax(0, "修改楼宇成功~", "", "");
        }
        #endregion

        //**************************2.0 批量编辑*****************************
        #region 2.0 批量编辑 + ActionResult SaveBatchData()
        /// <summary>
        ///  批量编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveBatchData()
        {
            int folder_id = RequestHelper.GetIntByName("folder_id");//导航id
            string fileIds = RequestHelper.GetStrByName("fileIds"),//作品id集合
                folder_path = RequestHelper.GetStrByName("folder_path");//导航path
            if(string.IsNullOrEmpty(fileIds))
                return operateContext.RedirectAjax(1, "待修改的作品id为空！", "", "");
            if (folder_id<=0 || string.IsNullOrEmpty(folder_path))
                return operateContext.RedirectAjax(1, "导航目录id异常！", "", "");
            try
            {
                string[] fileID = fileIds.Split(',');
                T_DocumentFolderRelation folderRel = new T_DocumentFolderRelation { folder_id = folder_id, folder_path = folder_path };
                //编辑导航目录
                foreach (string item in fileID)
                {
                    int file_id = Helper.ToInt(item);
                    operateContext.bllSession.T_DocumentFolderRelation.Modify(folderRel, s => s.file_id == file_id, "folder_id", "folder_path");
                }
                return operateContext.RedirectAjax(0, "数据更新成功~", "", "");
            }
            catch(Exception ex)
            {
                return operateContext.RedirectAjax(1, ex.Message, "", "");
            }
        }
        #endregion

        #region 2.1 初始化楼宇入住日志Table + ActionResult InitBuildLogTable()
        /// <summary>
        ///  初始化楼宇入住日志Table
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult InitBuildLogTable()
        {
            int page = RequestHelper.GetIntByName("page"),
                rows = RequestHelper.GetIntByName("rows"),
                file_id = RequestHelper.GetIntByName("file_id");
            int total;
            List<OutputBuildLog> list = operateContext.bllSession.T_VRStayUserLog.GetBuildLogList(file_id, page, rows, out total);
            TableModel tModel = new TableModel()
            {
                total = total,
                rows = list
            };
            return Json(tModel, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
